import { CourseDao } from '../dao/courseDao'
import { TrainerDao } from '../dao/trainerDao'
//import { ScenDao } from '../dao/scenDao'
import { Overview ,TrainerTotal} from '../../types/statistic'
import { VTrainerCourseDao } from '../dao/vTrainerCourseDao'
import { VTranRecordInfoDao } from '../dao/vTranRecordInfoDao'
import { ScensCount } from '../../types/vTrainerCourse'

export class StatisticService {

  //总览
  static async getStatistic(): Promise<Overview> {
    //总课程数
    const courseTotal = await CourseDao.countCourse()
    //已完成课程数
    const courseCompleteTotal = await CourseDao.countCourse(1,null)
    //未完成课程数
    const courseUnCompleteTotal = courseTotal - courseCompleteTotal
    //患者总数
    const trainerTotal = await TrainerDao.countTrainer()
    return {
      courseTotal,
      courseCompleteTotal,
      courseUnCompleteTotal,
      trainerTotal
    }
  }

  //今日工作统计
  static async getTodayWorkStatistic(){
    //获取当天日期，并转换为 yyyy-MM-dd
    const today = new Date().toISOString().split('T')[0]
    //今日总课程数
    const courseTotal = await CourseDao.countCourse(null,today)
    //今日已完成课程数
    const courseCompleteTotal = await CourseDao.countCourse(1,today)
    //今日未完成课程数
    const courseUnCompleteTotal = courseTotal - courseCompleteTotal
    //今日训练患者总数
    const trainerTotal = await VTrainerCourseDao.getTodayTrainerTotal(today)
    //今日已训练患者总数
    const trainerCompleteTotal = await VTranRecordInfoDao.getTodayTrainerTotal(today)

    return {
      courseTotal,
      courseCompleteTotal,
      courseUnCompleteTotal,
      trainerTotal,
      trainerCompleteTotal
    }
  }

  /**
   * 统计近7天已训练患者总数
   */
  static async getTrainerTotal(): Promise<TrainerTotal[]> {
    //获取从昨天开始的近7天的日期，返回数组
    const dates = []
    for (let i = 0; i < 7; i++) {
      const date = new Date()
      date.setDate(date.getDate() - i)
      dates.push(date.toISOString().split('T')[0])
    }
    //遍历日期数组，统计每个日期的已训练患者总数
    const trainerTotal: TrainerTotal[] = [] 
    for (const date of dates) {
      //console.log('date', date)
      const total = await VTranRecordInfoDao.getTodayTrainerTotal(date)
      trainerTotal.push({
        date,
        total
      })
    }
    return trainerTotal
  }

  /**
   * 训练场景次数排行
   */
  static async getScensTotal(): Promise<ScensCount[]> {
    const scensTotal = await VTrainerCourseDao.getScensTotal()
    //遍历场景数组，获取场景名称
   // for (const scens of scensTotal) {
   //   const scen = await ScenDao.getById(scens.secens_id)
   //   scens.scens_name = scen?.name || ''
    //}
    return scensTotal
  }

}